# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Zion Nimchuk <zionnimchuk@gmail.com>
# Contributor: Mateusz Mikuła <mati865@gmail.com

_realname=rust
_bootstrapping=yes
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.49.0
pkgrel=1
pkgdesc="Systems programming language focused on safety, speed and concurrency (mingw-w64)"
arch=('any')
url="https://www.rust-lang.org/"
license=('MIT' 'Apache')
depends=("${MINGW_PACKAGE_PREFIX}-gcc")
makedepends=("bison"
             "flex"
             "git"
             "${MINGW_PACKAGE_PREFIX}-cmake>=3.4.3"
             "${MINGW_PACKAGE_PREFIX}-curl"
             "${MINGW_PACKAGE_PREFIX}-libffi"
             "${MINGW_PACKAGE_PREFIX}-libssh2"
             "${MINGW_PACKAGE_PREFIX}-lldb"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-python"
             $([[ "$_bootstrapping" == "no" ]] && echo "${MINGW_PACKAGE_PREFIX}-rust")
             "${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs')
#install=rust.install
source=("https://static.rust-lang.org/dist/${_realname}c-${pkgver}-src.tar.gz"
        "force-curl-rust.patch"
        "0001-add-missing-libs.patch"
        "0002-fix-install-msys2.patch"
        "0003-link-with-system-curl.patch"
        "0004-unbundle-gcc.patch"
        "0005-win32-config.patch"
        "0006-Update-cc.patch")
sha256sums=('b50aefa8df1fdfc9bccafdbf37aee611c8dfe81bf5648d5f43699c50289dc779'
            '500f9bd7452c8d35115ad823d8fcb9bda8b86878c2a0f57f9e6a72fc4ba9a8f5'
            '20aae51f8fe525d677d81f4075011db4f413420393ccd212ba21d503880ddb61'
            '13cb1817ef83b396e16fe271172a77648bffe5e90efbbe98eb5fefc5a8d91860'
            'b16f65dfdc79e7a722b3e2589e90a1431a971038dd5dfa1394ec0427e3c0a2b4'
            '1a3d00f3bcf5c21beaa20ee729c45b7b3d76cd2a2c6b70562020f11c1e9dcc6d'
            '061f1cf5374c926bc130a4afbde005dcd09fdbaee079469ad045fd23a7a88a2c'
            '3c21cc518439f17944d368fcaf00648f048af4bcabc134f63acf47f32d8cc27b')
#validpgpkeys=('108F66205EAEB0AAA8DD5E1C85AB96E6FA1BE5FE') # Rust Language (Tag and Release Signing Key) <rust-key@rust-lang.org>
noextract=(${_realname}c-${pkgver}-src.tar.gz)

prepare() {
  [[ -d ${srcdir}/${_realname}c-${pkgver}-src ]] && rm -rf ${srcdir}/${_realname}c-${pkgver}-src
  tar -xzf ${srcdir}/${_realname}c-${pkgver}-src.tar.gz -C ${srcdir} || true

  cd ${srcdir}/${_realname}c-${pkgver}-src
  patch -p1 -i "${srcdir}/force-curl-rust.patch"
  patch -p1 -i "${srcdir}/0001-add-missing-libs.patch"
  patch -p1 -i "${srcdir}/0002-fix-install-msys2.patch"
  #patch -p1 -i "${srcdir}/0003-link-with-system-curl.patch"
  patch -p1 -i "${srcdir}/0004-unbundle-gcc.patch"
  patch -p1 -i "${srcdir}/0005-win32-config.patch"
  patch -p1 -i "${srcdir}/0006-Update-cc.patch"
}

build() {
  [[ -d "${srcdir}/${CARCH}" ]] && rm -rf "${srcdir}/${CARCH}"
  mkdir -p "${srcdir}/${CARCH}" && cd "${srcdir}/${CARCH}"

  #We have to do the following because rust doesn't count x86_64-w64-mingw32 as a target triple
  OSTYPE="$CARCH-pc-windows-gnu"

  local -a _rust_conf=()
  if [ "${_bootstrapping}" = "no" ]; then
    _rust_conf+=("--local-rust-root=$(cygpath -m ${MINGW_PREFIX})")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix;--sysconfdir;--localstatedir" \
  ../${_realname}c-${pkgver}-src/configure \
    --prefix=${MINGW_PREFIX} \
    --sysconfdir=${MINGW_PREFIX}/etc \
    --localstatedir=${MINGW_PREFIX}/var/lib \
    --build=$OSTYPE \
    --host=$OSTYPE \
    --target=$OSTYPE \
    --release-channel=stable \
    --enable-ninja \
    --enable-extended \
    --disable-codegen-tests \
    --llvm-root=${MINGW_PREFIX} \
    --python=${MINGW_PREFIX}/bin/python \
    ${_rust_conf}

  DEP_NGHTTP_ROOT=${MINGW_PREFIX} \
  DEP_OPENSSL_ROOT=${MINGW_PREFIX} \
  DEP_Z_ROOT=${MINGW_PREFIX} \
  ${MINGW_PREFIX}/bin/python ../${_realname}c-${pkgver}-src/x.py build --verbose --stage 2
}

check() {
  cd "${srcdir}/${CARCH}"
  # it might fail on Windows
  DEP_NGHTTP_ROOT=${MINGW_PREFIX} \
  DEP_OPENSSL_ROOT=${MINGW_PREFIX} \
  DEP_Z_ROOT=${MINGW_PREFIX} \
  ${MINGW_PREFIX}/bin/python ../${_realname}c-${pkgver}-src/x.py test --stage 2 || true
}

package() {
  cd "${srcdir}/${CARCH}"

  DEP_NGHTTP_ROOT=${MINGW_PREFIX} \
  DEP_OPENSSL_ROOT=${MINGW_PREFIX} \
  DEP_Z_ROOT=${MINGW_PREFIX} \
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/python ../${_realname}c-${pkgver}-src/x.py install --stage 2 --verbose

  rm -f ${pkgdir}${MINGW_PREFIX}/bin/libgcc*.dll
  rm -f ${pkgdir}${MINGW_PREFIX}/bin/libstd*.dll
  rm -f ${pkgdir}${MINGW_PREFIX}/bin/libwinpthread*.dll

  install -Dm644 ${srcdir}/${_realname}c-${pkgver}-src/LICENSE-APACHE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-APACHE"
  install -Dm644 ${srcdir}/${_realname}c-${pkgver}-src/LICENSE-MIT "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-MIT"

  install -d "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions"
  mv "${pkgdir}${MINGW_PREFIX}/etc/bash_completion.d/cargo" \
     "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/cargo"
}
